package com.online.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.entity.CorrectJsonObject;
import com.online.entity.ErrorJsonObject;
import com.online.entity.Student;
import com.online.entity.StudentClass;
import com.online.service.CEService;
import com.online.service.CourseContactClassService;
import com.online.service.StudentClassService;
import com.online.service.StudentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/studentClass")
public class StudentClassController {

    @Autowired
    StudentClassService studentClassService;

    @Autowired
    StudentService studentService;

    @Autowired
    CEService ceService;

    @Autowired
    CourseContactClassService courseContactClassService;

    /**
     * 查询当前教师所授班级列表
     *
     * @param teacherNumber
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getClass", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String getClass(@RequestParam(required = false) String teacherNumber) {

        Subject subject = SecurityUtils.getSubject();
        teacherNumber = (String) subject.getPrincipal();

        List<StudentClass> classList = studentClassService.showClassByTeacherNubmer(teacherNumber);
        if (classList.size() == 0) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(100, "暂未添加任何班级");
            return JSONObject.toJSONString(errorJsonObject);
        }

        if (classList != null) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(classList);
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101, "查询失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 查询当前教师所授班级并返回视图
     *
     * @param teacherNumber
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/getClassList", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String getClassList(@RequestParam(required = false) String teacherNumber, HttpServletRequest httpServletRequest) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        teacherNumber = (String) subject.getPrincipal();

        //查询当前老师所授班级列表并返回
        List<StudentClass> classList = studentClassService.showClassByTeacherNubmer(teacherNumber);

        if (classList.size() == 0) {
            httpServletRequest.setAttribute("result", "暂无添加班级");
            return "teacher/class_list";
        } else {
            httpServletRequest.setAttribute("classList", classList);
            return "teacher/class_list";
        }
    }

    /**
     * 教师删除班级信息
     *
     * @param classId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAClass", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String deleteClass(@RequestParam(value = "classId",required = false) Integer classId) {

        int result = studentClassService.deleteAStudentClass(classId);

        //查询该班级的课程信息
        List<Integer> list = courseContactClassService.selectCourseIdByClassId(classId);

        if(!list.isEmpty()) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(104,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(103,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 教师添加一条班级信息
     * @param className
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertAClass", method = RequestMethod.POST, produces = "text/html")
    public String insertAClass(@RequestParam(value = "className",required = false) String className) {

        Subject subject = SecurityUtils.getSubject();
        String teacherNumber = (String) subject.getPrincipal();

        StudentClass sc = studentClassService.showAClass(teacherNumber,className);
        if(sc != null) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(104,"添加失败");
            return JSONObject.toJSONString(errorJsonObject);
        }

        StudentClass studentClass = new StudentClass();
        //设置班级的教师
        studentClass.setTeacherNumber(teacherNumber);
        studentClass.setClassName(className);

        //插入班级
        int result = studentClassService.insertARecord(studentClass);

        if(result == 1) {

            List<Student> studentList = studentService.showByStudentClass(className);

//            为空则向前端提示添加学生信息
            if(studentList.isEmpty()) {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(103,"插入成功");
                return JSONObject.toJSONString(errorJsonObject);
            }else {
                CorrectJsonObject correctJsonObject = new CorrectJsonObject("插入成功");
                return JSONObject.toJSONString(correctJsonObject);
            }

        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(111,"添加失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 查询当前教师所授班级并返回学生管理界面
     *
     * @param teacherNumber
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/getStudentClassList", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String getStudentClassList(@RequestParam(required = false) String teacherNumber, HttpServletRequest httpServletRequest) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        teacherNumber = (String) subject.getPrincipal();

        //查询当前老师所授班级列表并返回
        List<StudentClass> classList = studentClassService.showClassByTeacherNubmer(teacherNumber);

        if (classList.size() == 0) {
            httpServletRequest.setAttribute("result", "暂无添加班级");
            return "teacher/student_class";
        } else {
            httpServletRequest.setAttribute("classList", classList);
            return "teacher/student_class";
        }
    }

    /**
     * 以携带参数的形式查询当前教师所授班级列表
     *
     * @param teacherNumber
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getGradeList", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String getClassList(@RequestParam(required = false) String teacherNumber) {

        List<StudentClass> classList = studentClassService.showClassByTeacherNubmer(teacherNumber);
        if (classList.size() == 0) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(100, "暂未添加任何班级");
            return JSONObject.toJSONString(errorJsonObject);
        }

        if (classList != null) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(classList);
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101, "查询失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

}
